<?php
class RoleController extends AdminController {
    public function listAction(){
        $model = new UserRoleModel;
        $this->_view->roles = $model->select();
        
    }
    public function createAction(){
        $model = new UserRuleModel;
        $this->_view->rules = $model->select();
    }
    public function insertAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        if($this->_request->isPost()){
            $model = new UserRoleModel;
            $flag = $model->add();
            if($flag){
                Response::success('nice');
            }else{
                Response::error('wrong');
            }
        }else{
            exit;
        }        
    }
    public function editAction(){
        $model = new UserRoleModel;
        $role = $model->getByUser();
        $this->_view->role = $role;
        $model = new UserRuleModel;
        // $this->_view->rules = $model->getRulesBySerialize($role['role_rules']);
        $this->_view->rules = $model->select();
    }
    public function updateAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        if($this->_request->isPost()){
            $role_id = Request::aspost(3,'id');
            if($role_id === null){
                exit;
            }else{
                $model = new UserRoleModel;
                $flag = $model->up($role_id);
                if($flag){
                    Response::success('nice');
                }else{
                    Response::error('wrong');
                }
            }
        }else{
            exit;
        }        
    }
    public function statusAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id','');
        if(empty($id)){
            die;
        }else{
            // Mark... Filter The 'status'
            $data['role_status']= isset($_POST['status']) ? $_POST['status'] : '';

            if($data['role_status'] == ''){
                return false;
            }else{
                $model = new UserRoleModel;
                $flag = $model->where(['role_id'=>$id])->update($data);
                if($flag == 0 || $flag == false){
                    Response::error('error');
                }else{
                    Response::success('nice');
                }
            }
        }        
    }        
    public function removeAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        if($this->_request->isPost()){
            $role_id = Request::aspost(3,'id');
            if($role_id === null){
                exit;
            }else{
                $model = new UserRoleModel;
                $flag = $model->delete($role_id);
                // $flag = false;
                if($flag){
                    Response::success('nice');
                }else{
                    Response::error('wrong');
                }
            }
        }else{
            exit;
        }           
    }
}